<template>
  <div class="w">
    <div class="top">
      <i id="ico" class="iconfont icon-xiangzuojiantou" @click="$router.go(-1)"></i>
      <span>修改权限</span>
      <p @click="rem">删除员工</p>
    </div>
    <div class="con">
      <van-form @submit="onSubmit">
        <van-field name="uploader" label="更换头像">
          <template #input>
            <van-uploader v-model="uploader" :max-count="1" :after-read="afterRead"/>
          </template>
        </van-field>
      <van-field
        v-model="lxName"
        name="性名"
        label="性名"
        placeholder="请填入性名"
        autocomplete="off"
        :rules="[{ required: true, message: '请填入性名' }]"
        />
      <van-field
        v-model="lxPhone"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        autocomplete="off"
        :rules="[{ required: true,
           message: '请填正确格式的手机号',
           pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
           }]"
       />
       <van-field name="authority" label="性别">
          <template #input>
            <van-radio-group v-model="authority" direction="horizontal">
              <van-radio name="1" shape="square" checked-color="#ff4c4c">上架车辆</van-radio>
              <van-radio name="2" shape="square" checked-color="#ff4c4c">修改车辆</van-radio>
              <van-radio name="3" shape="square" checked-color="#ff4c4c">下架车辆</van-radio>
              <van-radio name="4" shape="square" checked-color="#ff4c4c">分配商机</van-radio>
            </van-radio-group>
          </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ff4c4c">提交</van-button>
      </div>
    </van-form>
  </div>
  </div>
</template>

<script>
import { Revise, Delete } from '@/api/information'
import { Upload } from '@/api/upload'
import { Toast } from 'vant'
export default {
  data () {
    return {
      id: '',
      authority: '',
      lxPhone: '',
      lxName: '',
      pic: '',
      uploader: []
    }
  },
  methods: {
    async rem () {
      const data = await Delete({ id: this.id })
      if (data.success) {
        Toast(data.info)
        this.$router.push('/employee')
        // 清空表单
      } else {
        Toast(data.info + '删除失败')
      }
    },
    async afterRead (file) {
      // 此时可以自行将文件上传至服务器

      // 在这块创建FormData对象
      // FormData 对象的使用：
      // 1.用一些键值对来模拟一系列表单控件：即把form中所有表单元素的name与value组装成一个queryString
      // 2. 异步上传二进制文件。
      // 上传图片需要转换二进制这里要用到FormData
      const forms = new FormData()
      // 这里的file表示给后台传的属性名字，这里看接口文档需要传的的属性
      // 获取上传图片信息
      forms.append('file', file.file)
      const data = await Upload(forms)
      if (data.success) {
        this.pic = data.msg
      } else {
        Toast('上传失败')
      }
    },
    async onSubmit () {
      if (!this.authority) {
        Toast('请选择权限')
        // eslint-disable-next-line no-useless-return
        return
      }
      const forms = new FormData()
      forms.append('file', this.uploader[0].file)
      const res = {
        id: this.id,
        authority: this.authority,
        lxPhone: this.lxPhone,
        lxName: this.lxName,
        pic: this.pic
      }
      // console.log(res)
      // console.log(forms)
      const data = await Revise(res)
      if (data.success) {
        Toast(data.info)
        // 清空表单
        Object.assign(this.$data, this.$options.data())
      } else {
        Toast(data.info + '修改失败')
      }
      // console.log('提交')
    }
  },
  mounted () {
    this.id = this.$route.query.id
  }
}
</script>

<style lang="less" scoped>
.w{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  .top{
      width: 100%;
      height: 100px;
      box-sizing: border-box;
      padding: 0 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    >p{
        width: 115px;
        height: 57px;
        font-size: 24px;
        line-height: 57px;
        border-radius: 10px;
        text-align: center;
        color: #fff;
        background-color: #ff4c4c;
      }
  }
  .con{
    flex-grow: 1;
    >ul{
      list-style: none;
      >li{
        box-sizing: border-box;
        padding: 0 20px;
        border-radius: 10px;
        width: 680px;
        height: 100px;
        background-color: #f7f7f7;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        >span{
          font-size: 26px;
          color: #666666;
        }
        >span:last-child{
          color: #999999;
        }
      }
    }
  }
  .btn{
    width: 100%;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    >div{
      background-color: #ff4c4c;
      color: #ffffff;
      font-size: 30px;
      width: 680px;
      height: 80px;
      border-radius: 10px;
      line-height: 80px;
      text-align: center;
    }
  }
}
</style>
